<template>
  <SxDialog
    class="sxFilterFieldSetting"
    :visible="visible"
    :title="title"
    width="600px"
    hidden-padding
    @dialog-confirm="handleDialogConfirm"
    @dialog-close="handleDialogClose"
    @dialog-cancel="handleDialogClose"
  >
    <div class="sxFilterFieldSetting__title">
      <el-checkbox
        :indeterminate="checkIndeterminate"
        :value="checkAll"
        @change="handleCheckAll"
      >
        全选
      </el-checkbox>
    </div>
    <div ref="fieldList" class="sxFilterFieldSetting__fieldList">
      <div
        v-for="item in filteredData"
        :key="item.value"
        class="sxFilterFieldSetting__fieldItem"
      >
        <i class="iconfont icon-drop sxFilterFieldSetting__dropIcon"></i>
        <el-checkbox v-model="item.checked">
          {{ item.label }}
        </el-checkbox>
      </div>
    </div>
  </SxDialog>
</template>

<script>
import SxDialog from '@Components/Dialog'
import { Sortable } from 'sortablejs'
import { deepCopy } from '@Components/assets/js/common'

export default {
  components: {
    SxDialog
  },
  props: {
    title: String,
    visible: Boolean,
    data: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      filteredData: []
    }
  },
  computed: {
    checkIndeterminate() {
      return !this.checkAll && this.filteredData.some(i => i.checked)
    },
    checkAll() {
      return (
        this.filteredData.filter(i => i.checked).length ===
        this.filteredData.length
      )
    }
  },
  watch: {
    visible(val) {
      if (val) {
        // bind data
        this.filteredData = deepCopy(this.data)
        this.$nextTick(() => {
          this.bindSortable()
        })
      }
    }
  },
  mounted() {},
  methods: {
    handleDialogClose() {
      this.$emit('update:visible', false)
    },
    handleDialogConfirm() {
      this.$emit('update-filtered', this.getFilteredData())
      this.handleDialogClose()
    },
    handleCheckAll() {
      const bool = !this.checkAll
      this.filteredData.forEach(data => {
        data.checked = bool
      })
    },
    bindSortable() {
      Sortable.create(this.$refs.fieldList, {
        direction: 'vertical',
        onEnd: ({ newIndex, oldIndex }) => {
          // 交换原始数据的值
          const { filteredData } = this
          const moveData = filteredData.splice(oldIndex, 1)[0]
          filteredData.splice(newIndex, 0, moveData)
        }
      })
    },
    getFilteredData() {
      return this.filteredData
    }
  }
}
</script>

<style scoped lang="scss">
.sxFilterFieldSetting {
  &__title {
    padding: 6px 22px 6px 24px;
    border-bottom: 1px solid #f0f0f0;
    width: 100%;
    box-sizing: border-box;
  }
  &__dropIcon {
    padding-right: 6px;
    font-size: 12px;
    cursor: move;
  }

  &__fieldList {
    padding: 8px 6px;
    max-height: 400px;
    overflow-y: scroll;
  }
  &__fieldItem {
    padding: 4px 16px 4px 0;
    &.sortable-drag {
      opacity: 1 !important;
      background: #fff;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    }
  }
}
</style>
